package Command;

import java.util.ArrayList;
import java.util.Vector;

public class Command {
	
	public final static String[] number = new String[]{
		"0", 
		"1",
		"2",
		"3",
		"4",
		"5",
		"6",
		"7",
		"8",
		"9"
	};
	
	public final static String[] characters = new String[]{ "a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "x", "y", "z", "w" };
	
	public final static String[] symbols = new String[] { "~", "!", "@", "#", "$", "%", "^", "&", "*", "_", "+", "{", "}", ":", "\"", "|", "<", ">", "?", "-", "=", "[", "]", ";", "'", "\\", ",", ".", "/" };
	
	public final static String[] mainQuery = new String[]{
		"\nselect ",
		" select ",
		"\nselect\n",
		" select\n",		
		
		"\ninsert",
		" insert ",
		"\ninsert\n",
		" insert\n",
		
		"\nupdate ",
		" update ",
		"\nupdate\n",
		" update\n",
		
		"\ndelete ",
		" delete",
		"\ndelete\n",
		" delete\n"
	};
	
	public final static String[] exists = new String[]{
		"exists"
	};
	
	public final static String[] logicOperator = new String[]{
		"and",
		"or"
	};
	
	public final static String[] compareOperator = new String[]{		
		">=",
		"<=",
		"!=",
		"=",
		">",
		"<"
	};
	
	public final static String[] operator = new String[]{
		" > ",
		" >= ",
		" < ",
		" <= ",
		" and ",
		" or ",
		" = ",
		" != ",
		" in "		
	};
	
	public final static String[] supportQuery = new String[]{
		"\nwhere ",
		" where ",
		"\nwhere\n",
		" where\n",
		
		"\nfrom ",
		" from ",
		"\nfrom\n",
		" from\n",
		
		"\ngroup by ",
		" group by ",
		"\ngroup by\n",
		" group by\n",
		
		"\nhaving ",
		" having ",
		"\nhaving\n",
		" having\n",
	};

	public static Vector<Integer> countSingleQuote(String str)
	{
		Vector<Integer> indices = new Vector<Integer>();
		
		for(int i = 0 ; i < str.length(); i++)
		{
			if(str.charAt(i) == '\'')
			{
				indices.add(i);
			}
		}
		
		return indices;
	}
	
	public static void countCommand(ArrayList<Integer> commandIndices, String key, String fullText, int index)
	{
		int ret = fullText.indexOf(key, index);
		if (ret == -1)
			return;
		
		commandIndices.add(ret);
		
		countCommand(commandIndices, key, fullText, ret + 1);
	}
	
	public static boolean startWithAll(String str)
	{
		String trim = str.trim();
		if(trim.startsWith("all") && str.trim().endsWith("("))
		{
			return true;
		}
		
		return false;
	}
	
	public static boolean startWithAny(String str)
	{
		String trim = str.trim();
		if(trim.startsWith("any") && str.trim().endsWith("("))
		{
			return true;
		}
		
		return false;
	}
	
	public static boolean startWithOpenParentheses(String str)
	{
		String trim = str.trim();
		if(trim.startsWith("("))
			return true;
		
		return false;
	}
	
	public static boolean startWithOperator(String str)
	{
		String trim = str.trim();
		for(int i = 0 ; i < operator.length; i++)
			if(trim.startsWith(operator[i].trim()))
				return true;
		
		return false;
	}
	
	public static boolean endWithCloseParentheses(String str)
	{
		String trim = str.trim();
		if(trim.endsWith(")"))
			return true;
		
		return false;
	}
	public static boolean isHaveOperator(String str)
	{
		boolean ret = false;
		for(int i = 0 ; i < operator.length; i++)
			if(str.contains(operator[i]))
				ret = true;
		
		return ret;
	}
	
	public static int rightHandOfExistFunction(String str)
	{
		int ret = -1;
		for(int i = 0 ; i < exists.length; i++)
			if(str.indexOf(exists[i]) > -1)
				ret = str.indexOf(exists[i]) + exists[i].length();
		
		return ret;
		
	}
	
	public static int rightHandOfOperator(String str)
	{
		int ret = -1;
		for(int i = 0 ; i < operator.length; i++)
			if(str.contains(operator[i]))
				ret = str.indexOf(operator[i]) + operator[i].length();
		
		return ret;
	}
	
	public static boolean isHaveNumber(String str)
	{
		boolean ret = false;
		for(int i = 0 ; i < number.length; i++)
			if(str.contains(number[i]))
				ret = true;
		
		return ret;
	}
	
	public static boolean isHaveCharacters(String str)
	{
		boolean ret = false;
		for(int i = 0 ; i < characters.length; i++)
			if(str.contains(characters[i]))
				ret = true;
		
		return ret;
	}
	
	public static boolean isHaveSymbols(String str)
	{
		boolean ret = false;
		for(int i = 0 ; i < symbols.length; i++)
			if(str.contains(symbols[i]))
				ret = true;
		
		return ret;
	}
	
	public static boolean isHaveExistsFunction(String str)
	{
		boolean ret = false;
		for(int i = 0 ; i < exists.length; i++)
			if(str.contains(exists[i]))
				ret = true;
		
		return ret;
	}
	
	public static boolean isHaveGroupBy(String str)
	{
		boolean ret = false;
		for(int i = 8 ; i < 12; i++)
			if(str.contains(supportQuery[i]))
				ret = true;
		
		return ret;
	}
	
	public static boolean isHaveHaving(String str)
	{
		boolean ret = false;
		for(int i = 12 ; i < 16; i++)
			if(str.contains(supportQuery[i]))
				ret = true;
		
		return ret;
	}
	
	public static boolean isHaveWhere(String str)
	{
		boolean ret = false;
		for(int i = 0 ; i < 4; i++)
			if(str.contains(supportQuery[i]))
				ret = true;
		
		return ret;
	}
	
	public static boolean isSelect(String str)
	{
		boolean ret = false;
		for(int i = 0 ; i < 4; i++)
			if(str.contains(mainQuery[i]))
				ret = true;
		
		return ret;
	}
	
	public static boolean isDelete(String str)
	{
		boolean ret = false;
		for(int i = 12 ; i < 16; i++)
			if(str.contains(mainQuery[i]))
				ret = true;
		
		return ret;
	}
	
	public static boolean isInsert(String str)
	{
		boolean ret = false;
		for(int i = 4 ; i < 8; i++)
			if(str.contains(mainQuery[i]))
				ret = true;
		
		return ret;
	}
	
	public static boolean isUpdate(String str)
	{
		boolean ret = false;
		for(int i = 8 ; i < 12; i++)
			if(str.contains(mainQuery[i]))
				ret = true;
		
		return ret;
	}
	
	public final static String[] keywords = new String[]{
		"create",
		"database",
		"select",
		"update",
		"into",
		"delete",
		"values",
		"value",
		"set",
		"where",
		"from",
		"distinct",
		"between",
		"like",
		"in",
		"and",
		"or",
		"order",
		"by",
		"des",
		"desc",
		"asc",
		"as",
		"not",
		"exists",
		"any",
		"all",
		"insert"
	};
	
	private String sql;
	private String dbName = "TruongHoc";
	
	public Command()
	{
		this.sql = "";
	}
		
	public Command(String sql)
	{
		this.sql = sql;
	}

	public String getSql() {
		return sql;
	}

	public String getDbName() {
		return dbName;
	}

	public void setDbName(String dbName) {
		this.dbName = dbName;
	}
	
	public void setSql(String sql) {
		this.sql = sql;
	}
	
	public void mergeSql(String nextSql) 
	{
		this.sql += nextSql;
	}
	
	public Object executeQuery()
	{
		return null;
	}
}
